home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 8_13.lha / 8_13 / 8_13c.c < prev    next >
C/C++ Source or Header  |  1993-08-08  |  1KB  |  51 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. / Match a pattern on the input stream.
  6. / Use the UNIX System V regcmp(3) routines
  7. / to do full regular expression matching.
  8.  
  9. include <pat.h>    /* DELETE */
  10. include <ctype.h>    /* DELETE */
  11. include <limits.h>    /* DELETE */
  12. include <stdlib.h>    /* DELETE */
  13. xtern char *regcmp(char* ...);
  14. xtern int regex(char *re, char *subj, char *ret0 ...);
  15.  
  16. nt pat::match(char *pattern)
  17.  
  18.    // reset base ptr to pt to beginning of buffer
  19.    ebp->setbuffering();
  20.  
  21.    // make sure that a full line's worth is available
  22.    for (int c = ebp->sgetc();
  23.  (c != '\n') && (c != EOF);
  24.  c = ebp->sgetc())
  25. ebp->stossc();
  26.    int foundnewline = (c == '\n');
  27.  
  28.    // null out the newline
  29.    if (foundnewline)
  30. {
  31. ebp->stossc();
  32. ebp->sputbackc(0);
  33. }
  34.  
  35.    // compile the pattern
  36.    char *re = regcmp(pattern, (char*)0);
  37.  
  38.    // check the line against the compiled pattern
  39.    int goodpattern = regex(re, ebp->base, 0);
  40.  
  41.    // put the newline back
  42.    if (foundnewline)
  43. {
  44. ebp->stossc();
  45. ebp->sputbackc('\n');
  46. }
  47.  
  48.    ebp->resetbuffering();
  49.    return goodpattern;
  50.  
  51.